package com.panshi.service.impl;

import com.panshi.dao.IFunctionDao;
import com.panshi.dao.IRoleDao;
import com.panshi.service.IRoleService;
import com.panshi.tools.TransitionUtils;
import com.panshi.vo.FunctionVO;
import com.panshi.vo.KeyValueVO;
import com.panshi.vo.ResultVO;
import com.panshi.vo.RoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl implements IRoleService {

    @Resource
    private IRoleDao iRoleDao;

    @Override
    public ResultVO<List<RoleVO>> findRoleList(int status) {
        ResultVO<List<RoleVO>> resultVO = new ResultVO();
        List<RoleVO> list = iRoleDao.findRoleList(status);
        resultVO.setData(list);
        return resultVO;
    }

    @Override
    public ResultVO<List<KeyValueVO>> getRoleSelectByUserId(long id) {
        ResultVO<List<KeyValueVO>> resultVO = new ResultVO<>();
        List<RoleVO> list = iRoleDao.getRoleSelectByUserId(id);
        resultVO.setData(TransitionUtils.getRoleKeyValueVO(list));
        return resultVO;
    }

    @Override
    public ResultVO<List<KeyValueVO>> initRoleListInfo() {
        // 状态 1正常
        int status = 1;
        ResultVO resultVO = new ResultVO();
        List<RoleVO> list = iRoleDao.findRoleList(status);
        resultVO.setData(TransitionUtils.getRoleKeyValueVO(list));
        return resultVO;
    }

    @Override
    public ResultVO<String> editRoleStatus(RoleVO roleVO) {
        log.info("要更改状态的角色信息{}", roleVO);
        int status = iRoleDao.editRoleStatus(roleVO);
        ResultVO<String> resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("一切正常");
        return resultVO;
    }

    @Override
    public ResultVO<String> delRoleInfo(long roleId) {
        // 首先删除角色的权限
        int delRoleFunctionStatus = iRoleDao.delRoleFunctionInfo(roleId);
        // 删除角色
        int delStatus = iRoleDao.delRoleInfo(roleId);
        ResultVO<String> resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("一切正常");
        return resultVO;
    }

    @Override
    public ResultVO<String> addRoleInfo(RoleVO roleVO) {
        ResultVO<String> resultVO = new ResultVO<>();
        RoleVO roleVO1 = iRoleDao.getByNameRoleInfo(-1, roleVO.getRoleName());
        if (roleVO1 == null) {
            int addRoleStatus = iRoleDao.addRoleInfo(roleVO);
            log.info("新增角色内容------>" + roleVO);
            String[] funIds = roleVO.getFunIds().split(",");
            // 给角色添加权限
            int addRoleFunctionStatus = iRoleDao.addRoleFunctionInfo(roleVO.getId(), funIds);

            resultVO.setCode("200");
            resultVO.setMsg("已成功添加角色" + roleVO.getRoleName());
        } else {
            resultVO.setCode("201");
            resultVO.setMsg("该角色已经存在啦,请重新输入吧！");
        }
        return resultVO;
    }

    @Override
    public ResultVO<String> editRoleInfo(RoleVO roleVO) {
        ResultVO<String> resultVO = new ResultVO<>();
        RoleVO roleVO1 = iRoleDao.getByNameRoleInfo(roleVO.getId(), roleVO.getRoleName());
        if(roleVO1 == null){
            // 更改角色信息
            int editRoleStatus = iRoleDao.editRoleInfo(roleVO);
            //更改前先把角色权限删除了
            int delrRoleStatus = iRoleDao.delRoleFunctionInfo(roleVO.getId());
            // 更改角色权限
            String[] funIds = roleVO.getFunIds().split(",");

            int editRoleFunctionStatus = iRoleDao.editRoleFunctionInfo(roleVO.getId(),funIds);
            resultVO.setCode("200");
            resultVO.setMsg("修改成功");
        }else{
            resultVO.setCode("201");
            resultVO.setMsg("当前角色已存在请重新输入！");
        }
        return resultVO;
    }
}
